﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Omron.Actors;
using Omron.Framework;

namespace Omron.GameStates
{
    partial class OmronMainStageV1
    {
        void InitMenu()
        {
            bar = new Bar(new Vector2(330, GraphicsDevice.PresentationParameters.BackBufferHeight - 50), 50, 10);
            bar.BackColor = Color.SteelBlue * 0.2f;
            bar.ForeColor = Color.SteelBlue;
            playerF.UpdateFactionMenu += playerF_UpdateFactionMenu;

            actButs = new ListBox(100, 70, UIMan, 10, GraphicsDevice.PresentationParameters.BackBufferHeight - 100);
            actButs.ItemPressed += new MenuItemPressed(actButs_ItemPressed);

            factionMenu = new ListBox(120, 80, UIMan, 20,
                        GraphicsDevice.PresentationParameters.BackBufferHeight - 100);
            factionMenu.ItemPressed += new MenuItemPressed(factionMenu_ItemPressed);

            playerF.UpdateBuildingMenus += new Action(playerF_UpdateBuildingMenus);

            playerF_UpdateFactionMenu();

            world.NotifyActorRemoved += new ActorRemoved(world_NotifyActorRemoved);
        }

        void playerF_UpdateBuildingMenus()
        {
            if (curMenuAct != null)
            {
                curMenuAct.MenuNeedsUpdate();
            }
        }

        void world_NotifyActorRemoved(Actor actor)
        {
            if (activeUnits.Contains(actor))
            {
                actor.IsSelected = false;
                activeUnits.Remove(actor);
                updateSelectedAndMenu();
            }
        }

        void playerF_UpdateFactionMenu()
        {
            List<Texture2D> buildImgs = new List<Texture2D>();
            foreach (string build in playerF.BuildingsUnlocked)
            {
                UnitsAndBuilduings.BuilduingTypeInfo info = ResourceManager.Resources[build];
                Texture2D image = ResourceManager.Resources[info.IdleAnimation.Animation][0];
                buildImgs.Add(image);
            }
            factionMenu.SetItems(playerF.BuildingsUnlocked.ToArray(), buildImgs.ToArray());
            updateSelectedAndMenu();
        }

        public void SetMenu(Actor act)
        {
            if (curMenuAct != null)
                act.UpdateMenu -= act_UpdateMenu;
            act.UpdateMenu += act_UpdateMenu;
            curMenuAct = act;
            setMenu();
        }

        void setMenu()
        {
            clearMenu();
            menu = curMenuAct.Menu;
            if (menu != null)
            {
                actButs.SetItems(menu.Commands);
                bar.Value = menu.BarValue;
                UIMan.AddControl(bar);

                actButs.Show();
                info = new Text(new Vector2(400, GraphicsDevice.PresentationParameters.BackBufferHeight - 90), "", "UnitMenuFont");
                info.Color = Color.Wheat;
                UIMan.AddControl(info);
            }
        }


        void actButs_ItemPressed(int itemKey)
        {
            menu.OnCommandInvoked(itemKey);
        }

        void act_UpdateMenu()
        {
            setMenu();
        }
        

        public void ClearMenu()
        {
            if (menu == null)
                return;
            curMenuAct.UpdateMenu -= act_UpdateMenu;
            curMenuAct = null;
            clearMenu();
        }

        void clearMenu()
        {
            actButs.Hide();
            UIMan.RemoveControl(info);
            UIMan.RemoveControl(bar);
            menu = null;
        }

        public void UpdateActMenu()
        {
            if (menu == null)
                return;
            info.TextMsg = menu.Info;
            if (menu.DisplayBar)
            {
                if (!UIMan.ContainsControl(bar))
                    UIMan.AddControl(bar);
                bar.Value = menu.BarValue;
            }
            else
            {
                if (UIMan.ContainsControl(bar))
                    UIMan.RemoveControl(bar);
            }
        }

        ActorMenu menu;
        Text info;
        ListBox actButs;
        Bar bar;
        ListBox factionMenu;
        Actor curMenuAct;




        void updateSelectedAndMenu()
        {
            if (activeUnits.Count == 0)
            {
                clearIcons();
                ClearMenu();
                factionMenu.Show();
            }
            else if (activeUnits.Count == 1)
            {
                clearIcons();
                SetMenu(activeUnits[0]);
                factionMenu.Hide();
            }
            else
            {
                ClearMenu();
                updateSelectedIcons();
                factionMenu.Hide();
            }
        }




        //different stuff
        void clearIcons()
        {
            if ((actIcons != null) && (actIcons.Count != 0))
            {
                UIMan.RemoveRange(actIcons);
                actIcons.Clear();
            }
        }

        List<Control> actIcons;
        void updateSelectedIcons()
        {
            clearIcons();
            actIcons = new List<Control>();
            int wid = 30;
            int height = 30;
            int begX = 50;
            int maxX = minMapRect.Left - wid * 6;

            int curX = begX;
            int curY = GraphicsDevice.PresentationParameters.BackBufferHeight - height * 3;
            foreach (Actor act in activeUnits)
            {
                ActorIcon icon = new ActorIcon(curX, curY, wid, height, act);
                actIcons.Add(icon);
                icon.IconMouseUp += new IconPressedEventHandler(icon_IconMouseUp);
                UIMan.AddControl(icon);
                curX += wid;
                if (curX > maxX)
                {
                    curX = begX;
                    curY += height;
                }
            }
        }

        void icon_IconMouseUp(ActorIcon icon)
        {
            if (UIMan.CtrlDown)
            {
                activeUnits.Remove(icon.Actor);
                icon.Actor.IsSelected = false;
            }
            else if (UIMan.AltDown)
            {
                SelectType(icon.Actor);
            }
            else
            {
                UnselectUnits();
                activeUnits.Add(icon.Actor);
                icon.Actor.IsSelected = true;
            }
            updateSelectedAndMenu();
        }
    }
}
